//
//  TestView.m
//  TestWebViewPagable
//
//  Created by zhuxuhong on 2016/12/22.
//  Copyright © 2016年 zhuxh. All rights reserved.
//

#import "TestView.h"

@implementation TestView
{
    NSMutableArray<UIView*> *_contentViews;
}


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.multipleTouchEnabled = false;
        self.backgroundColor = [UIColor redColor];
        self.layer.masksToBounds = true;
        
        _contentViews = [NSMutableArray array];
        for (int i=0; i<5; i++) {
            [_contentViews addObject:[self coverViewWithIndex:i]];
        }
        
        [self addSubview:_contentViews[0]];
        
        // 添加滑动手势
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(actionOfPan:)];
        [self addGestureRecognizer:pan];
        
    }
    return self;
}

-(UIView*)coverViewWithIndex: (NSInteger)index{
    UIView *v = [[UIView alloc] initWithFrame:self.bounds];
    v.backgroundColor = [UIColor greenColor];
    v.layer.shadowColor = [UIColor blackColor].CGColor; // 阴影颜色
    v.layer.shadowOffset = CGSizeMake(0, 0); // 偏移距离
    v.layer.shadowOpacity = 0.5; // 不透明度
    v.layer.shadowRadius = 10.0; // 半径
    
    UILabel *label = [self label];
    label.text = [NSString stringWithFormat:@"Cover %ld",index];
    [v addSubview:label];
    v.userInteractionEnabled = false;
    return v;
}

-(UILabel*)label{
    UILabel *label = [[UILabel alloc] initWithFrame:self.bounds];
    label.font = [UIFont systemFontOfSize:100];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

-(void)actionOfPan: (UIPanGestureRecognizer*)pan
{
    CGFloat location = [pan locationInView:pan.view].x;

    static BOOL initial = true;
    static CGFloat beginX;
    static CGFloat lastX;
    static BOOL directionToLeft = false;
    static NSInteger current = 0;
    static NSInteger total = 5;
    static BOOL viewAdded = false;
    static UIView *tempView = nil;
    static CGRect frame;
    static BOOL animating = false;
    static BOOL noOperating; //不允许操作
    
    switch (pan.state)
    {
        case UIGestureRecognizerStateBegan:
        {
            NSLog(@"began - current: %ld",current);
//            NSLog(@"began x: %f translation x: %f",location,translation);
            viewAdded = false;
            noOperating = false;
            beginX = 0;
            lastX = location;
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            if (animating) {
                return;
            }
            
            //1. 方向判断
            CGFloat delta = location - lastX;
            if (delta == 0) {
                return;
            }
            if (delta < 0) {
//                NSLog(@"<----");
                directionToLeft = true;
            }
            else{
//                NSLog(@"---->");
                directionToLeft = false;
            }
            lastX = location;
            
            //2. 条件判断
            if (initial && current == 0 && !directionToLeft) {
//                NSLog(@"第一页 --->");
                noOperating = true;
                viewAdded = true;
                return;
            }
            if(initial && current == total-1 && directionToLeft){
//                NSLog(@"最后一页 <---");
                noOperating = true;
                viewAdded = true;
                return;
            }
//            NSLog(@"changed x: %f translation x: %f",location,translation);
            
            //3. 添加view
            if (!viewAdded && !directionToLeft) { //向右
                tempView = _contentViews[current-1];
                [self addSubview:tempView];
                viewAdded = true;
                beginX = 0;
                frame = tempView.frame;
            }
            else if(!viewAdded && directionToLeft){ //向左
                tempView = [self.subviews lastObject];
                [self insertSubview:_contentViews[current+1] belowSubview:tempView];
                frame = tempView.frame;
                beginX = CGRectGetMaxX(frame) - location;
                viewAdded = true;
            }
            
            //4. 改变frame
            frame.origin.x = location + beginX - CGRectGetWidth(frame);
            if (frame.origin.x > 0) {
                frame.origin.x = 0;
            }
            tempView.frame = frame;
            initial = false;
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            if (animating || noOperating) {
                return;
            }
//            NSLog(@"ended x: %f translation x: %f",location,translation);
            
            //1. 进行动画
            animating = true;
            frame.origin.x = -CGRectGetWidth(frame);
            if (!directionToLeft) {
                frame.origin.x = 0;
            }
            
            // 动画时间别太长
            [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
//                self.backgroundColor = [UIColor blackColor];
                tempView.frame = frame;
            } completion:^(BOOL finished) {
                if (finished)
                {
                    //2. view的移除
                    if (directionToLeft) { // <---
                        [tempView removeFromSuperview];
                        UIView *v = [self.subviews lastObject];
                        if (!v) {
                            return;
                        }
                        tempView = v;
                    }
                    else if(!directionToLeft){ // --->
                        UIView *v = [self.subviews firstObject];
                        if (!v) {
                            return;
                        }
                        [v removeFromSuperview];
                    }
                    
                    //3. current改变
                    NSInteger cur = [_contentViews indexOfObject:tempView];
                    if (cur < 0 || cur > total-1){
                        return;
                    }
                    if (cur != current) {
                        current += directionToLeft ? 1 : -1;
                        current = current < 0 ? 0 : current;
                        current = current > total-1 ? total-1 : current;
                    }
                    NSLog(@"finished - current: %ld",current);
                    
                    //4. 参数重设
                    animating = false;
                    lastX = 0;
                    initial = current == 0 || current == total-1;
                }
                else{
                    animating = true;
                }
            }];
        }
            break;
        default:
        {

        }
            break;
    }
}

@end
